//
//  assetsOrderSellViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/7.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "assetsOrderSellViewController.h"
#import "CommonData.h"
#import "AssetsOrderingTableViewCell.h"
#import "JiaoYiPageDetailViewController.h"
#import "taskOverviewService.h"
#import "OrderQiuGouTableViewCell.h"
#import <Masonry.h>
#import "JiaoYiPageDetailsView.h"

@interface assetsOrderSellViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView      *tableView;
@property (nonatomic,assign)NSInteger      page;
@property (nonatomic,strong)    NSArray<dataOrderBuyAndSaleInfo *> *  orderList;
@end

@implementation assetsOrderSellViewController

///////出售中

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    [self loadOrder:1];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.hidesBottomBarWhenPushed = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    [self setBlackStatus:YES];
}
-(void)loadData{
    self.page = 1;
    [self loadOrder:1];
}

-(void)loadMoreData{
    self.page ++;
    [self loadOrder:self.page];
}

-(void)loadOrder:(NSInteger) pageIndex{
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setField:[NSString stringWithFormat:@"%ld",self.page] value:@"page"];
    [dict setField:@"10" value:@"pageSize"];
    [dict setField:@"1" value:@"orderType"];//交易类型：0 求购 1 出售
    [dict setValue:[UserCenter shareCenter].user.userNo forKey:@"userNo"];
    [taskOverviewService getOrderBuyAndSaleList:dict success:^(dataOrderBuyAndSale *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            self.orderList = response.orderList;
            [self.tableView reloadData];
            return ;
        }
        [DKProgressHUD showErrorWithStatus:response.statusDesc];
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}


#pragma mark - tableview

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.orderList count];
}

-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 74;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete;
}
//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"取消订单";
}
//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //在这里实现删除操作 //删除数据，和删除动画
    dataOrderBuyAndSaleInfo *m =  [self.orderList objectAtIndex:indexPath.row];
    dataOrderBuyAndSaleInfo *mm = [dataOrderBuyAndSaleInfo mj_objectWithKeyValues:m];
    [DKProgressHUD showLoading];
    NSMutableDictionary *dict=  [NSMutableDictionary new];
    [dict setValue:mm.orderNo forKey:@"orderNo"];
    [taskOverviewService getCancelOrder:dict success:^(BaseModel *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"取消订单成功"];
            [self loadOrder:0];
        }else{
            [DKProgressHUD showErrorWithStatus:response.statusDesc];
        }
        
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
    
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderQiuGouTableViewCell *cell = [OrderQiuGouTableViewCell cellWithTableView:tableView IndexPath:indexPath];
    dataOrderBuyAndSaleInfo *m =  [self.orderList objectAtIndex:indexPath.row];
    dataOrderBuyAndSaleInfo *mm = [dataOrderBuyAndSaleInfo mj_objectWithKeyValues:m];
    cell.labelCNY.text = [NSString stringWithFormat:@"%@ CNY",mm.rmbAmount];
    cell.labelGTE.text = [NSString stringWithFormat:@"%@ GTE",mm.gteCoin];
    cell.labelTime.text = mm.orderTime;
    cell.labelPrice.text = [NSString stringWithFormat:@"单价：%@CNY  限额：%@CNY",mm.price,mm.limitAmount];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    dataOrderBuyAndSaleInfo *m =  [self.orderList objectAtIndex:indexPath.row];
    dataOrderBuyAndSaleInfo *mm = [dataOrderBuyAndSaleInfo mj_objectWithKeyValues:m];
    JiaoYiPageDetailViewController *vc = [JiaoYiPageDetailViewController new];
//    vc.modelSaleInfo = mm;
    vc.isQiuGou = NO;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
